package com.zwz.crawler.riskInfo;

import com.zwz.constant.Constant;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.Map;

import static com.zwz.constant.Constant.ENTERPRISE_ID;

@Slf4j
public class TungeeTaxpayerAbnormal {
    public static void  startTungeeTaxpayerAbnormal( ) {
        // 构建请求参数
//        String enterpriseId = "577de19eadf21dc4";
        String url = "https://sales.tungee.com/api/enterprise/taxpayer/abnormal/list"
                + "?enterprise_id=" + ENTERPRISE_ID;

        try {
            // 发送请求
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    // 设置请求头（与浏览器完全一致）
                    .header("authority", "sales.tungee.com")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/business-risk")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    // 设置完整 Cookie（需替换为最新值）
//                    .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; __last_enter_version=sales; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b84fe276-099f-4932-92b3-d54b92ae384b; _tx_sid=MTc0NTMwMTE0MzYyMjo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301094,1745302709; HMACCOUNT=B4E402DF057C895E; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuSXhVPSI.GujnjA.YBo4Zvc8vs4jgEmtioacknOQ9pE; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745311256; acw_tc=1a0c384f17453698184017570e00605a98e1a4c90bfaaa7d86f0d745f0d5de; accountCenterSessionId=.eJwlzUFLxDAQhuH_kvMeZjLTJLs3tYsXt72sCL2USTKhllKh7aoo_neLHl_4eL5vkwaZZ53MyXxoNAdzW3XpX_PeLoCvHCSnSePRUVRGtNbuo1k196u8a7-99TmaU5Fp1YPpy6LrYE7bcttr_WPaehiar4Rd_by1dTd0DwDttRmfXs6fzeNla8a76nLtpqY-UzfeDzv__09CoQTECErZUS5YgkPFrFFj8eiVJCQbM7CwMqeUvBcCsCAJjgDgS6AcAoswko8kRark2aYKSGLmCjJGBxzAOsbMasEWpV3OntT8_AIOQFR_.GunM6Q.8QBdr-Xkfzi0uQiWY91xsUwqsys; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNzAxZGRkY2E5NDE1MjJiNzdlODY0In0.GunM-w.CGkfmJdQV2Xi1xrpII364hvBbDg")
                    .header("cookie", Constant.cookies)
                    .ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .maxBodySize(0)
                    .timeout(30000)
                    .execute();

            // 处理响应
           log.info("HTTP状态码: " + response.statusCode());
           //log.info("响应内容:\n" + response.body());
            // 错误处理
            handleErrorResponse(response);

            //解析
            JsonParserUtil.parseTaxPayer2(response.body());
        } catch (IOException e) {
            System.err.println("请求异常: " + e.getMessage());
            e.printStackTrace();
        }


    }

    private static void handleErrorResponse(Connection.Response response) {
        switch (response.statusCode()) {
            case 401:
                System.err.println("[认证失败] 可能原因：");
                System.err.println("1. Cookie失效（需重新登录获取）");
                System.err.println("2. 缺少动态签名参数（检查是否需要 x-tonxis-signature）");
                break;
            case 403:
                System.err.println("[权限不足] 可能原因：");
                System.err.println("1. 当前账户无访问该企业数据的权限");
                System.err.println("2. 企业ID归属验证失败");
                break;
            case 404:
                System.err.println("[接口不存在] 请检查URL路径是否正确");
                System.err.println("当前路径: " + response.url());
                break;
            case 429:
                System.err.println("[请求频繁] 建议：");
                System.err.println("1. 添加3-5秒请求间隔");
                System.err.println("2. 使用代理IP轮换");
                break;
            default:
                if (response.statusCode() != 200) {
                    System.err.println("未知错误码: " + response.statusCode());
                }
        }
    }


    // 登录方法示例
//    public static Map<String, String> refreshCookies() throws IOException {
//        return Jsoup.connect("https://user.tungee.com/login")
//                .data("phone", "13800138000")
//                .data("password", "encrypted_pass")
//                .post()
////                .cookies(); //todo test
//    }

// 在请求中自动应用
//.cookies(refreshCookies())

}